     package com.handong.handserialprogram.module.others;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.base.BackStackFragment;
import com.handong.handserialprogram.utils.JLog;
import com.handong.handserialprogram.utils.SystemUtil;

import java.util.ArrayList;
import java.util.List;

/**
 */
public class HelpDetailsFragment extends BackStackFragment {

    protected static final String TAG = HelpDetailsFragment.class.getSimpleName();
    private int mHelpType = 0;
    protected static final int HELP_USER_ABOUT = 1;
    protected static final int HELP_CREATE_MISSION = 2;
    protected static final int HELP_SIMPLE_MISSION = 3;
    protected static final int HELP_SPECIAL_FUNCTION = 4;
    protected static final int HELP_HAND_LOGINOUT=5;

    private ViewPager mViewPager;
    private TextView mDescribe, mProgressShow;
    private int[] mDescribeContent;

    public static HelpDetailsFragment newInstance(int type) {
        HelpDetailsFragment fragment = new HelpDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TAG, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mHelpType = bundle.getInt(TAG);
        }
    }

    @Override
    protected int getContentViewID() {
        return R.layout.fragment_help_user_details;
    }

    @Override
    protected void initViewByID(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.helpContentViewPager);
        mDescribe = (TextView) view.findViewById(R.id.helpDescribeContent);
        mProgressShow = (TextView) view.findViewById(R.id.helpProgressText);
    }

    @Override
    protected void initViewData() {
        JLog.d(TAG, "height:" + SystemUtil.getStatusBarHeight(getContext()));
        int[] imageID = null;
        switch (mHelpType) {
            case HELP_USER_ABOUT:
                initHeaderLayout(R.string.title_user_about);
                imageID = new int[]{R.drawable.help_login_one,
                        R.drawable.help_login_two};
                mDescribeContent = new int[]{R.string.help_user_login_step_two,
                        R.string.help_user_login_step_three};
                initViewPager(imageID, mDescribeContent);
                break;
            case HELP_SIMPLE_MISSION :
                initHeaderLayout(R.string.title_help_user_mission);
                imageID = new int[]{R.drawable.help_create_mission_step_one, R.drawable.help_create_mission_step_two,
                        R.drawable.help_create_mission_step_three, R.drawable.help_create_mission_step_four,
                        R.drawable.help_create_mission_step_five,R.drawable.help_create_mission_step_six};
                mDescribeContent = new int[]{R.string.help_user_mode_mission, R.string.help_user_mode_mission_second,
                        R.string.help_user_mode_mission_third, R.string.help_user_mode_mission_four,
                        R.string.help_user_mode_mission_five,R.string.help_user_mode_mission_six};
                initViewPager(imageID, mDescribeContent);
                break;
            case HELP_CREATE_MISSION:
                initHeaderLayout(R.string.title_help_simple_mission);
                imageID = new int[]{R.drawable.help_simple_mission_step_one, R.drawable.help_create_mission_step_three,
                        R.drawable.help_simple_mission_step_three, R.drawable.help_simple_mission_step_four,R.drawable.help_simple_mission_step_five};
                mDescribeContent = new int[]{R.string.help_simple_mission_one, R.string.help_simple_mission_second,
                        R.string.help_simple_mission_third, R.string.help_simple_mission_four,R.string.help_simple_mission_five};
                initViewPager(imageID, mDescribeContent);
                break;
            case HELP_SPECIAL_FUNCTION:
                initHeaderLayout(R.string.title_help_emergency);
                imageID = new int[]{R.drawable.help_emergency_step_one, R.drawable.help_emergency_step_two,
                        R.drawable.help_emergency_step_three};
                mDescribeContent = new int[]{R.string.help_user_emergency_step_one, R.string.help_user_emergency_step_two,
                        R.string.help_user_emergency_step_three};
                initViewPager(imageID, mDescribeContent);
                break;
                case HELP_HAND_LOGINOUT:
                    initHeaderLayout(R.string.title_help_loginout);
                    imageID = new int[]{R.drawable.help_loginout_step_one, R.drawable.help_loginout_step_two};
                    mDescribeContent = new int[]{ R.string.help_loginout_step_one, R.string.help_loginout_step_two}  ;
                    initViewPager(imageID, mDescribeContent);
                    break;
            default:
        }
    }

    private void initViewPager(int[] imageID, final int[] tipID) {
        mProgressShow.setText(1 + "/" + mDescribeContent.length);
        mDescribe.setText(tipID[0]);
        ContentPagerAdapter adapter = new ContentPagerAdapter(initImageRes(imageID));
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mDescribe.setText(mDescribeContent[position]);
                mProgressShow.setText((position + 1) + "/" + tipID.length);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private List<FrameLayout> initImageRes(int[] resID) {
        List<FrameLayout> list = new ArrayList<>();
        ImageView imageView = null;
        FrameLayout layout = null;
        FrameLayout.LayoutParams parent = null;
        for (int i = 0; i < resID.length; i++) {
            imageView = new ImageView(getContext());
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(387,
                    688);
            params.gravity = Gravity.CENTER_HORIZONTAL;
            imageView.setLayoutParams(params);
            imageView.setImageResource(resID[i]);

            layout = new FrameLayout(getContext());
            params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            layout.setLayoutParams(params);
            layout.addView(imageView);
            list.add(layout);
        }
        return list;
    }

    @Override
    public void onDestroyView() {
        mDescribeContent = null;
        super.onDestroyView();
    }

    class ContentPagerAdapter extends PagerAdapter {

        private List<FrameLayout> mList;

        public ContentPagerAdapter(List<FrameLayout> list) {
            mList = list;
        }

        @Override
        public int getCount() {
            return mList != null ? mList.size() : 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            FrameLayout frameLayout = mList.get(position);
            container.addView(frameLayout, 0);
            return frameLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
    }
}
